package com.xiong.demo;

import com.xiong.excel.ExcelUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.junit.Test;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.*;

public class TestDemo {

    @Test
    public void Test1(){
        LocalDate startDate = LocalDate.of(2024, 1, 14); // 起始日期
        LocalDate endDate = LocalDate.of(2024, 2, 24); // 结束日期
        String[] strings={"8:00-18:00","8:00-24:00","24:00-9:00","休息","休息"};

        LocalDate start = LocalDate.of(2024, 2, 9);
        LocalDate end = LocalDate.of(2024, 2, 17);

        // 遍历日期范围
        LocalDate currentDate = startDate;
        int i=0;

        List<Map<String,Object>> mapList=new ArrayList<>();
        while (currentDate.isBefore(endDate) || currentDate.equals(endDate)) {
            Map<String,Object> map=new HashMap<>();
            DayOfWeek dayOfWeek = currentDate.getDayOfWeek();
            String weekday = dayOfWeek.getDisplayName(TextStyle.FULL, Locale.getDefault());
            System.out.println(currentDate+" "+strings[i]+" "+weekday);
            map.put("日期",currentDate.toString());
            map.put("上班状态",strings[i]);
            map.put("星期",weekday);
            mapList.add(map);
            currentDate = currentDate.plusDays(1); // 增加一天
            i++;
            if(i%strings.length==0){
                i=0;
            }
        }
        ExcelUtils.createExcel(mapList,"C:\\Users\\86186\\Desktop\\fsdownload\\123.xls",(k)->{
            String headerValue=k.getHeaderValue();
            Cell cell=k.getCell();
            if("星期".equals(headerValue)){
                Object value=k.getValue();
                if("星期六".equals(value) || "星期日".equals(value)){
                   CellStyle cellStyle=k.createCellStyle();
                   cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
                   cell.setCellStyle(cellStyle);
                }
            }
            if("日期".equals(headerValue)){
                Object value=k.getValue();
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                LocalDate date = LocalDate.parse(value.toString(), formatter);
                boolean isWithinRange = date.isAfter(start) && date.isBefore(end);
                if(isWithinRange){
                    CellStyle cellStyle=k.createCellStyle();
                    cellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
                    cell.setCellStyle(cellStyle);
                }
            }
        });
    }



}
